package com.ledim.ledimview;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.ledim.activity.RoomActivity;
import com.ledim.activity.RoomDescActivity;
import com.ledim.activity.RoomNameActivity;
import com.ledim.bean.ChatRoomBean;
import com.ledim.bean.LedimUserBean;
import com.ledim.service.coverter.LedimGsonConverterFactory;
import com.ledim.utils.FrescoUtil;
import com.ledim.utils.LedimSpUtils;
import com.letv.android.young.client.R;

public class RoomTitleView extends LinearLayout implements View.OnClickListener {

    private View mNameLayout;
    private View mDescLayout;
    private View mLayoutNext;
    private View mUserLayout;
    private TextView mNameText;
    private SimpleDraweeView mImage;
    private ImageView mImageNext;
    private TextView mDescText;
    private TextView mRoomOwner;
    private TextView mRoomCoins;
    private TextView mCoverModify;
    private AlphaAnimation mAlphaAnimation;
    private LedimUserIconView user_icon_view;
    private ChatRoomBean mRoom;
    private boolean mIsMe;
    public int mHeight;
    public int mWidth;
    private Context mContext;
    public boolean pIsChangeCover;

    public RoomTitleView(Context context) {
        super(context);
        mContext = context;
    }

    public RoomTitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public RoomTitleView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        mWidth = wm.getDefaultDisplay().getWidth();
        mHeight = mWidth * 9 / 16;
        //        init();
    }

    //绑定数据
    public void bindData(boolean mIsMe, ChatRoomBean mRoom) {
        this.mRoom = mRoom;
        this.mIsMe = mIsMe;
        init();
    }

    /**
     * 初始化界面
     */
    private void init() {
        mNameLayout = findViewById(R.id.room_name_layout);
        mDescLayout = findViewById(R.id.room_desc_layout);
        mUserLayout = findViewById(R.id.letv_fragment_player_user_view);
        mLayoutNext = findViewById(R.id.layout2);
        mNameText = (TextView) findViewById(R.id.room_name);
        mDescText = (TextView) findViewById(R.id.room_desc);
        mCoverModify = (TextView) findViewById(R.id.room_cover_modify);
        mRoomOwner = (TextView) findViewById(R.id.letv_fragment_player_user_name);
        mRoomCoins = (TextView) findViewById(R.id.letv_fragment_player_user_gold);
        mImage = (SimpleDraweeView) findViewById(R.id.room_cover);
        mImageNext = (ImageView) findViewById(R.id.image2);
        user_icon_view = (LedimUserIconView) findViewById(R.id.user_icon_view);
        ViewGroup.LayoutParams params = mImage.getLayoutParams();
        params.height = mHeight;
        params.width = mWidth;
        mImage.setLayoutParams(params);

        if (mIsMe) {
            mNameLayout.setOnClickListener(this);
            mDescLayout.setOnClickListener(this);
            mUserLayout.setVisibility(View.GONE);
            user_icon_view.setVisibility(GONE);
            if (mRoom.name != null && mRoom.name.length() > 0) {
                mNameText.setText(mRoom.name);
            } else {
                String userStr = LedimSpUtils.getUser();
                if (userStr != null) {
                    try {
                        LedimUserBean user =
                                LedimGsonConverterFactory.create().gson.fromJson(userStr, LedimUserBean.class);
                        mNameText.setText(user.nickname + "的放映厅");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            mCoverModify.setVisibility(View.VISIBLE);
            handler.sendEmptyMessageDelayed(0, 3000);
        } else {//别人看
            if (mRoom.name != null && mRoom.name.length() > 0) {
                mNameText.setText(mRoom.name);
            }
            mUserLayout.setVisibility(View.VISIBLE);
            user_icon_view.setVisibility(View.VISIBLE);
            mNameLayout.setVisibility(View.GONE);
            mRoomOwner.setVisibility(View.VISIBLE);
            mRoomOwner.setText(mRoom.user.nickname);
            mLayoutNext.setVisibility(View.GONE);
        }
        if (mRoom.user.avatar != null && !TextUtils.isEmpty(mRoom.user.avatar.thumb)) {
            user_icon_view.setUserIcon(mRoom.user.userId, mRoom.user.status, mRoom.user.avatar.thumb, true);
        } else {
            user_icon_view.setUserIcon(mRoom.user.userId, mRoom.user.status, "", true);
        }
        if (mRoom.intro != null && mRoom.intro.length() > 0 && !mRoom.intro.equals("null")) {
            mDescText.setText(mRoom.intro);
        } else {
            mDescText.setText("独污污与众污污，熟乐啊少年~~");
        }
        if (mRoom.cover != null && !mRoom.cover.thumb.equals("null") && mRoom.cover.thumb != null &&
                mRoom.cover.thumb.length() > 0) {
            FrescoUtil.loadUrl(mRoom.cover.thumb, mImage);
            pIsChangeCover = true;
        } else {
            pIsChangeCover = false;
            mImage.setImageResource(R.drawable.default_image);
        }

        if (mRoom.user.coin != null) {
            mRoomCoins.setText(mRoom.user.coin);
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (mContext != null) {
                mAlphaAnimation = new AlphaAnimation(1f, 0f);
                mAlphaAnimation.setDuration(500);
                mCoverModify.startAnimation(mAlphaAnimation);
                mCoverModify.setVisibility(View.GONE);
            }
        }
    };
    public void setRoomTitle(String name) {
        mNameText.setText(name);

    }

    public void setRoomDesc(String name) {
        mDescText.setText(name);

    }

    public boolean getRoomCover() {
        return pIsChangeCover;

    }

    public void setRoomCover(Bitmap bm) {
        mImage.setVisibility(View.VISIBLE);
        pIsChangeCover = true;
        mImage.setImageBitmap(bm);
    }

    public void setRoomCover(String url) {
        mImage.setVisibility(View.VISIBLE);
        pIsChangeCover = true;
        FrescoUtil.loadUrl(url, mImage);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.room_name_layout:
                intent = new Intent(mContext, RoomNameActivity.class);
                intent.putExtra(RoomNameActivity.ROOM_ID, mRoom.roomId);
                intent.putExtra(RoomNameActivity.ROOM_NAME, mNameText.getText().toString());
                ((Activity) mContext).startActivityForResult(intent, RoomActivity.REQUESTCODE_ROOM);
                break;
            case R.id.room_desc_layout:
                intent = new Intent(mContext, RoomDescActivity.class);
                intent.putExtra(RoomDescActivity.ROOM_ID, mRoom.roomId);
                intent.putExtra(RoomDescActivity.ROOM_NAME, mDescText.getText().toString());
                ((Activity) mContext).startActivityForResult(intent, RoomActivity.REQUESTCODE_ROOM);
                break;
            
            /*
            * LedimApiServiceUtils.modifyRoomInfo(mRoomId, null, mDescContent, new LedimHttpObserver<BaseResultDataInfo<ChatRoomBean>>() {
                        @Override
                        public void onError(Throwable e) {
                            super.onError(e);
                        }

                        @Override
                        public void onNext(
                                BaseResultDataInfo<ChatRoomBean> chatRoomBeanBaseResultDataInfo) {
                            super.onNext(chatRoomBeanBaseResultDataInfo);
                            if (chatRoomBeanBaseResultDataInfo.success) {
                                ToastUtil.toast("放映厅描述修改成功");
                                Intent intent = new Intent();
                                intent.putExtra("content", mDescContent);
                                // 设置结果，并进行传送
                                RoomDescActivity.this.setResult(REQUESTCODE_ROOM_DESC, intent);
                                finish();
                            }
                        }
                    });
            * */
            
            
        }
    }

}
